package com.lailai.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lailai.entity.Route;
import com.lailai.entity.RouteConfig;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 导航栏持久层
 *
 * @Author lgr
 * @Date 2025/6/27
 * @Wechat a983105834
 * @Email 983105834@qq.com
 * @Copyright <a href="www.lailai.ink">lailai个人空间</a>
 */
@Mapper
public interface RouterMapper extends BaseMapper<Route> {
    /**
     * 根据路由ID查询配置信息
     */
    @Select("SELECT * FROM routes_config WHERE route_id = #{routeId}")
    RouteConfig selectByRouteId(@Param("routeId") Long routeId);

    /**
     * 查询所有路由及其配置信息
     */
    @Select("SELECT " +
            "r.id, r.parent_id, r.name, r.path, r.component_path, r.level, " +
            "r.sort_order, r.status, r.redirect, r.props, r.create_time, r.update_time, " +
            "rc.id as 'routesConfig.id', " +
            "rc.route_id as 'routesConfig.routeId', " +
            "rc.alias_of as 'routesConfig.aliasOf', " +
            "rc.title as 'routesConfig.title', " +
            "rc.icon as 'routesConfig.icon', " +
            "rc.no_cache as 'routesConfig.noCache', " +
            "rc.affix as 'routesConfig.affix', " +
            "rc.breadcrumb as 'routesConfig.breadcrumb', " +
            "rc.hidden as 'routesConfig.hidden', " +
            "rc.roles as 'routesConfig.roles', " +
            "rc.sort as 'routesConfig.sort', " +
            "rc.status as 'routesConfig.status', " +
            "rc.visible as 'routesConfig.visible', " +
            "rc.requires_auth as 'routesConfig.requiresAuth', " +
            "rc.keep_alive as 'routesConfig.keepAlive', " +
            "rc.permissions as 'routesConfig.permissions', " +
            "rc.before_enter as 'routesConfig.beforeEnter', " +
            "rc.create_time as 'routesConfig.createTime', " +
            "rc.update_time as 'routesConfig.updateTime' " +
            "FROM routes r " +
            "LEFT JOIN routes_config rc ON r.id = rc.route_id " +
            "ORDER BY r.parent_id, r.sort_order")
    // 修改这里：r.sort → r.sort_order
    List<Route> selectRoutesWithConfig();
}
